home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / demos / GL / flip / flipobj.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  5KB  |  222 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. #include <math.h>
  18. #include "stdio.h"
  19. #include "gl.h"
  20. #include "device.h"
  21.  
  22. #include "flip.h"
  23. #include "hash.h"
  24.  
  25. flipobj
  26. *readflipobj(name)
  27. char *name;
  28. {
  29.     FILE    *inf;
  30.     flipobj    *obj;
  31.     int        i, j;
  32.     int        nlongs;
  33.     int        magic;
  34.     int        *ip;
  35.  
  36.     inf = fopen(name,"r");
  37.     if(!inf) {
  38.         fprintf(stderr,"readfast: can't open input file %s\n",name);
  39.         exit(1);
  40.     }
  41.     fread(&magic,sizeof(int),1,inf);
  42.     if(magic != FASTMAGIC) {
  43.     fprintf(stderr,"readfast: bad magic in object file\n");
  44.     fclose(inf);
  45.         exit(1);
  46.     }
  47.     obj = (flipobj *)malloc(sizeof(flipobj));
  48.     fread(&obj->npoints,sizeof(int),1,inf);
  49. /*** IGNORE COLORS FIELD ***/
  50.     fread(&magic,sizeof(int),1,inf);
  51.  
  52.     /*
  53.      * Insure that the data is quad-word aligned and begins on a page
  54.      * boundary.  This shields us from the performance loss which occurs 
  55.      * whenever we try to fetch data which straddles a page boundary  (the OS
  56.      * has to map in the next virtual page and re-start the DMA transfer).
  57.      */
  58.     nlongs = 8 * obj->npoints;
  59.     obj->data = (float *) malloc(nlongs*sizeof(int) + 4096);
  60.     obj->data = (float *) (((int)(obj->data)) + 0xfff);
  61.     obj->data = (float *) (((int)(obj->data)) & 0xfffff000);
  62.  
  63.     for (i = 0, ip = (int *)obj->data;  i < nlongs/4;  i++, ip += 4)
  64.         fread(ip, 3 * sizeof(int), 1, inf);
  65.     fclose(inf);
  66.  
  67. /*
  68.  *    This has to be done first
  69.  */
  70.     swirl_randomize(obj);
  71.     find_edges(obj);
  72.  
  73.     return obj;
  74. }
  75.  
  76. void
  77. drawflipobj(obj)
  78. flipobj *obj;
  79. {
  80.     register float *p,*end;
  81.     enum DrawType lflag;
  82.  
  83.     p = obj->data;
  84.     end = p + 8 * obj->npoints;
  85.     lflag = obj->type;
  86.  
  87.     if (obj->type == POLYGONS)
  88.     {
  89.         while ( p < end) {
  90.             bgnpolygon();
  91.             n3f(p);
  92.             v3f(p+4);
  93.             n3f(p+8);
  94.             v3f(p+12);
  95.             n3f(p+16);
  96.             v3f(p+20);
  97.             n3f(p+24);
  98.             v3f(p+28);
  99.             endpolygon();
  100.             p += 32;
  101.         }
  102.     }
  103.     else
  104.     {
  105.         int i;
  106.         
  107.         for (i = 0 ; i < obj->nedges; i++)
  108.         {
  109.             bgnline();
  110.             n3f(obj->edge[i].v0 - 4);
  111.             v3f(obj->edge[i].v0);
  112.             n3f(obj->edge[i].v1 - 4);
  113.             v3f(obj->edge[i].v1);
  114.             endline();
  115.         }
  116.     }
  117. }
  118.  
  119.  
  120. /*
  121.  * objmaxpoint
  122.  *
  123.  * find the vertex farthest from the origin,
  124.  * so we can set the near and far clipping planes tightly.
  125.  */
  126.  
  127. #define MAXVERT(v) if ( (len = sqrt(    (*(v))  *  (*(v))  +      \
  128.                     (*(v+1)) * (*(v+1)) +           \
  129.                     (*(v+2)) * (*(v+2)) )) > max)  \
  130.             max = len;
  131.  
  132. float
  133. objmaxpoint(obj)
  134. flipobj *obj;
  135. {
  136.     register float *p, *end;
  137.     register int npolys;
  138.     register float len;
  139.     register float max = 0.0;
  140.  
  141.     p = obj->data;
  142.  
  143.     end = p + 8 * obj->npoints;
  144.     while ( p < end) {
  145.         MAXVERT(p+4);
  146.         MAXVERT(p+12);
  147.         MAXVERT(p+20);
  148.         MAXVERT(p+28);
  149.         p += 32;
  150.     }
  151.  
  152.     return max;
  153. }
  154.  
  155. /*
  156.  *    Use hash functions to find all unique edges
  157.  */
  158. find_edges(obj)
  159. flipobj *obj;
  160. {
  161.     int i, j, v0, v1, n;
  162.     float *p, *end;
  163.  
  164.     h_init_vertex(obj->npoints * 2);
  165.     h_init_edge(obj->npoints * 4);
  166.  
  167. /* First run through, to figure out how many there are */
  168.     p = obj->data;
  169.     end = p + 8 * obj->npoints;
  170.     while ( p < end) {
  171.         v0 = h_find_vertex(p+4);
  172.         v1 = h_find_vertex(p+12);
  173.         h_find_edge(v0, v1);
  174.         v0 = h_find_vertex(p+12);
  175.         v1 = h_find_vertex(p+20);
  176.         h_find_edge(v0, v1);
  177.         v0 = h_find_vertex(p+20);
  178.         v1 = h_find_vertex(p+28);
  179.         h_find_edge(v0, v1);
  180.         v0 = h_find_vertex(p+28);
  181.         v1 = h_find_vertex(p+4);
  182.         h_find_edge(v0, v1);
  183.         p += 32;
  184.     }
  185. /* Now malloc enough space */
  186.     obj->nedges = h_get_ne();
  187.     obj->edge = (flipedge *)malloc(sizeof(flipedge)*obj->nedges);
  188.  
  189. /* And now run through, filling up structure */
  190.     p = obj->data;
  191.     end = p + 8 * obj->npoints;
  192.     while ( p < end) {
  193.         v0 = h_find_vertex(p+4);
  194.         v1 = h_find_vertex(p+12);
  195.         n = h_find_edge(v0, v1);
  196.         obj->edge[n].v0 = p+4;
  197.         obj->edge[n].v1 = p+12;
  198.         
  199.         v0 = h_find_vertex(p+12);
  200.         v1 = h_find_vertex(p+20);
  201.         n = h_find_edge(v0, v1);
  202.         obj->edge[n].v0 = p+12;
  203.         obj->edge[n].v1 = p+20;
  204.  
  205.         v0 = h_find_vertex(p+20);
  206.         v1 = h_find_vertex(p+28);
  207.         n = h_find_edge(v0, v1);
  208.         obj->edge[n].v0 = p+20;
  209.         obj->edge[n].v1 = p+28;
  210.  
  211.         v0 = h_find_vertex(p+28);
  212.         v1 = h_find_vertex(p+4);
  213.         n = h_find_edge(v0, v1);
  214.         obj->edge[n].v0 = p+28;
  215.         obj->edge[n].v1 = p+4;
  216.  
  217.         p += 32;
  218.     }
  219.     h_destroy_vertex();
  220.     h_destroy_edge();
  221. }
  222.